package com.example.echoewish.ui.main_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.pojo.HotSearch;
import com.example.echoewish.pojo.PartPost;
import com.example.echoewish.repository.PostMessageRepository;
import com.example.echoewish.util.DataGenerator;
import com.example.echoewish.util.LogUtil;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchContentViewModel extends ViewModel {
    public MutableLiveData<String> keyWordMutableLiveData;
    MutableLiveData<List<PartPost>> partPostMutableLiveData;
    MutableLiveData<List<HotSearch>> hotSearchMutableLiveData;

    public SearchContentViewModel()
    {
        keyWordMutableLiveData=new MutableLiveData<>();
        partPostMutableLiveData=new MutableLiveData<>();
        hotSearchMutableLiveData=new MutableLiveData<>();
    }

    public void getPartPostMessageByKeyWord()
    {
            PostMessageRepository.getInstance().queryPartPostByKeyWord(keyWordMutableLiveData.getValue(),new getPartPostCallBack());
    }

    public void getHotSearch()
    {
        PostMessageRepository.getInstance().getHotSearch(Constant.HOT_SEARCH_NUM,new HotSearchCallBack());
    }

    private class getPartPostCallBack implements Callback<List<PartPost>>
    {

        @Override
        public void onResponse(Call<List<PartPost>> call, Response<List<PartPost>> response) {
            partPostMutableLiveData.postValue(response.body());
            new LogUtil().i(LogUtil.CALLBACK_TAG,"获取帖子部分消息 回调成功");
        }

        @Override
        public void onFailure(Call<List<PartPost>> call, Throwable t) {
            partPostMutableLiveData.postValue(null);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取帖子部分消息 回调失败"+t);
        }
    }

    private class HotSearchCallBack implements Callback<List<HotSearch>>
    {

        @Override
        public void onResponse(Call<List<HotSearch>> call, Response<List<HotSearch>> response) {
            hotSearchMutableLiveData.postValue(response.body());
        }

        @Override
        public void onFailure(Call<List<HotSearch>> call, Throwable t) {
            hotSearchMutableLiveData.postValue(null);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"获取热搜回调出现错误"+t);
        }
    }
}
